const app = getApp();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
};

var currentPage = {
  data,
  form:{},
  onLoad,
  uploadImages,
  changeAgree,
  comfirm,
};

var page = Object.assign({}, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const json = JSON.parse(options.json);
  that.form=json;
  that.setData(json);
}

function uploadImages() {
  const that = this;
  app.uploadFile.uploadImages({
    count: 1, //上传图片张数
    uploadSuccess: function(res) {
      // 上传回调。包括成功及失败
      // res回调参数(json数组形式返回)
      // 对应图片上传成功的对应数组元素包括width,height,pic
      // 对应图片上传失败对应的数组元素为{}
      console.log(res)
      that.setData({
        license: res[0].pic
      })
      that.form.license = res[0].pic;
    }
  })
}

function changeAgree() {
  const that = this;
  let temp = {};
  let isAgree = that.data.isAgree;
  temp.isAgree = isAgree == 1 ? 0 : 1;
  that.setData(temp);
  that.form.isAgree = temp.isAgree;
}

function comfirm(){
  const that = this;
  const isAgree = that.data.isAgree;
  const license = that.data.license;

  if (!license) return app.middleware.showToast("请上传营业执照");
  if (isAgree != 1) return app.middleware.showToast("请同意协议");

  app.middleware.requestAjax({
    serAddr: app.serAddrs.addOrUpdate,
    data: that.form,
    success: function (res) {
      wx.redirectTo({
        url: app.xhx_route.applyDetail,
      })
    },
    fail: function (res) {
      app.middleware.showToast( res && res.msg || "请求失败");
    }
  })
}